package com.jyxd.web.service.zauto.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyxd.web.dao.basic.MedOrderExecDao;
import com.jyxd.web.dao.zauto.ZAutoAdviceTaskDao;
import com.jyxd.web.data.zauto.ZAutoAdviceTaskEntity;
import com.jyxd.web.data.zauto.form.ZAutoAdviceQueryForm;
import com.jyxd.web.data.zauto.form.ZAutoAssociatedAdviceForm;
import com.jyxd.web.service.zauto.ZAutoAdviceTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ZAutoAdviceTaskServiceImpl extends ServiceImpl<ZAutoAdviceTaskDao, ZAutoAdviceTaskEntity> implements ZAutoAdviceTaskService {

    @Resource
    private MedOrderExecDao medOrderExecDao;

    @Override
    public Page<ZAutoAdviceTaskEntity> getList(ZAutoAdviceQueryForm zAutoAdviceQueryForm) {
        // 创建分页对象
        Page<ZAutoAdviceTaskEntity> page = new Page<>(zAutoAdviceQueryForm.getStart(), zAutoAdviceQueryForm.getPageSize());
        LambdaQueryWrapper<ZAutoAdviceTaskEntity> zAutoAdviceTaskEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zAutoAdviceTaskEntityLambdaQueryWrapper.eq(ZAutoAdviceTaskEntity::getPatientId, zAutoAdviceQueryForm.getPatientId());
        if (zAutoAdviceQueryForm.getDisabled() != null) {
            zAutoAdviceTaskEntityLambdaQueryWrapper.eq(ZAutoAdviceTaskEntity::getDisabled, zAutoAdviceQueryForm.getDisabled());
        }
        // 执行分页查询
        return this.page(page, zAutoAdviceTaskEntityLambdaQueryWrapper);
    }

    @Override
    public List<ZAutoAssociatedAdviceForm> adviceList(String patientId, String adviceType) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        map.put("isSync", 5);
        // 创建一个 SimpleDateFormat 对象，指定日期格式为 '%Y%m%d'
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 获取当前日期
        Date now = new Date();
        // 格式化当前日期为字符串
        String timeOrder = sdf.format(now);

        map.put("timeOrder", timeOrder);
        map.put("repeatIndicator", adviceType);
        List<Map<String, Object>> list = medOrderExecDao.getList(map);
        return list.stream().map(record -> {
            ZAutoAssociatedAdviceForm zAutoAssociatedAdviceForm = new ZAutoAssociatedAdviceForm();
            zAutoAssociatedAdviceForm.setOrderName((String) record.get("orderName"));
            zAutoAssociatedAdviceForm.setDrugType((Integer) record.get("drugType"));
            zAutoAssociatedAdviceForm.setRepeatIndicator((Integer) record.get("repeatIndicator"));
            zAutoAssociatedAdviceForm.setId((String) record.get("id"));
            return zAutoAssociatedAdviceForm;
        }).collect(Collectors.toList());
    }
}
